package com.huike.web.controller.review;


import com.huike.common.core.controller.BaseController;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.core.page.TableDataInfo;
import com.huike.review.pojo.Review;
import com.huike.review.service.ReviewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 该Controller主要是为了复习三层架构以及Mybatis使用的，该部分接口已经放开权限，可以直接访问
 * 同学们在此处编写接口通过浏览器访问看是否能完成最简单的增删改查
 */
@RestController
@RequestMapping("/review")
public class MybatisReviewController extends BaseController {

    @Autowired
    private ReviewService reviewService;

    /**=========================================================新增数据============================================*/
    @GetMapping("/saveData/{name}/{age}/{sex}")
    public AjaxResult saveData(@PathVariable String name,@PathVariable Integer age,@PathVariable String sex){
        System.out.println(name);
        int i = reviewService.saveData(name, age, sex);
        if(i>0){
            return new AjaxResult(200,"成功插入:1条数据");
        }
        return AjaxResult.error(500,"插入失败");
    }

    @PostMapping("/saveData")
    public AjaxResult saveData(@RequestBody Review review){
        int i = reviewService.saveData(review);
        if(i>0){
            return new AjaxResult(200,"成功插入:1条数据");
        }
        return AjaxResult.error(500,"插入失败");

    }
    /**=========================================================删除数据=============================================*/
    @DeleteMapping("/remove/{id}")
     public AjaxResult remove(@PathVariable Long id){
        int remove = reviewService.remove(id);
        if(remove>0){
            return new AjaxResult(200,"成功删除:1条数据");
        }
        return AjaxResult.error(500,"删除失败");
    }
    /**=========================================================修改数据=============================================*/
      @PostMapping("/update")
      public AjaxResult update(@RequestBody Review review){
          int update = reviewService.update(review);
          if(update>0){
              return new AjaxResult(200,"修改成功");
          }
          return AjaxResult.error(500,"修改失败");
      }
    /**=========================================================查询数据=============================================*/
       @GetMapping("/getById")
       public AjaxResult getById(Long id){
           Review byId = reviewService.getById(id);
           if(byId!=null){
               return new AjaxResult(200,"操作成功",byId);
           }
           return AjaxResult.error(500,"操作失败");
       }

       @GetMapping("/getDataByPage")
       public TableDataInfo getDataByPage(int pageNum, int pageSize){
           List<Review> dataByPage = reviewService.getDataByPage(pageNum, pageSize);
           if(dataByPage!=null&&dataByPage.size()>0){
               return getDataTable(dataByPage);
           }
           TableDataInfo tableDataInfo = new TableDataInfo();
           tableDataInfo.setCode(500);
           tableDataInfo.setMsg("操作失败");
           return tableDataInfo;
       }
}